package services

import (
	"go.uber.org/zap"
	"iot-base/common/ctrltype"
	"iot-base/common/logger"
	"iot-base/devc/common/loader"
)

type Service struct {
}

func NewService() *Service {
	return &Service{}
}

// SubService 订阅服务.
func (s *Service) SubService() {
	loader.SubCtrlHandler(func(pCmd *ctrltype.CtrlCmd) (map[string]interface{}, error) {
		if pCmd != nil {
			if f, ok := handlerMap[pCmd.ID]; ok {
				defer func() {
					if r := recover(); r != nil {
						logger.Log.Error("SubService SubCtrlHandler", zap.Error(r.(error)))
					}
				}()
				return f(pCmd)
			}
		}
		return nil, nil
	})
}
